<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * TYPOlight Open Source CMS
 * Copyright (C) 2005-2010 Leo Feyer
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 * @copyright  2010 Andreas Koob
 * @author     Andreas Koob
 * @package    tasks
 * @license    LGPL
 * @filesource
 */


/**
 * Add palette
 */
$GLOBALS['TL_DCA']['tl_content']['palettes']['__selector__'][] = 'tm_use_fixed_project';
$GLOBALS['TL_DCA']['tl_content']['palettes']['__selector__'][] = 'tm_use_fixed_task';

$GLOBALS['TL_DCA']['tl_content']['palettes']['tm_projectlist'] = '{title_legend},name,headline,type;{tm_settings},tm_redirect_project';
$GLOBALS['TL_DCA']['tl_content']['palettes']['tm_tasklist'] = '{title_legend},name,headline,type;{tm_settings},tm_use_fixed_project,tm_redirect_task';
$GLOBALS['TL_DCA']['tl_content']['palettes']['tm_taskreader'] = '{title_legend},name,headline,type;{tm_settings},tm_use_fixed_task';

$GLOBALS['TL_DCA']['tl_content']['subpalettes']['tm_use_fixed_project'] = 'tm_fixed_project';
$GLOBALS['TL_DCA']['tl_content']['subpalettes']['tm_use_fixed_task'] = 'tm_fixed_task';
/**
 * Add fields
 */
$GLOBALS['TL_DCA']['tl_content']['fields']['tm_fixed_task'] = array
(
	'label'                   => &$GLOBALS['TL_LANG']['tl_content']['tm_fixed_task'],
	'exclude'                 => false,
	'inputType'               => 'select',
	'eval'                    => array('mandatory'=>true),
	'options_callback'        => array('tl_content_tsk', 'getTasks')
);

$GLOBALS['TL_DCA']['tl_content']['fields']['tm_fixed_project'] = array
(
	'label'                   => &$GLOBALS['TL_LANG']['tl_content']['tm_fixed_project'],
	'exclude'                 => false,
	'inputType'               => 'select',
	'eval'                    => array('mandatory'=>true),
	'options_callback'        => array('tl_content_tsk', 'getProjects')
);

$GLOBALS['TL_DCA']['tl_content']['fields']['tm_redirect_project'] = array
(
	'label'                   => &$GLOBALS['TL_LANG']['tl_content']['tm_redirect_project'],
	'exclude'                 => false,
	'inputType'               => 'pageTree',
	'eval'                    => array('mandatory'=>true,'fieldType'=>'radio', 'tl_class'=>'clr')
);

$GLOBALS['TL_DCA']['tl_content']['fields']['tm_redirect_task'] = array
(
	'label'                   => &$GLOBALS['TL_LANG']['tl_content']['tm_redirect_task'],
	'exclude'                 => false,
	'inputType'               => 'pageTree',
	'eval'                    => array('mandatory'=>true,'fieldType'=>'radio', 'tl_class'=>'clr')
);

$GLOBALS['TL_DCA']['tl_content']['fields']['tm_use_fixed_project'] = array
(
	'label'                   => &$GLOBALS['TL_LANG']['tl_content']['tm_fixed_project'],
	'exclude'                 => false,
	'inputType'               => 'checkbox',
	'eval'                    => array('submitOnChange'=>true)
);

$GLOBALS['TL_DCA']['tl_content']['fields']['tm_use_fixed_task'] = array
(
	'label'                   => &$GLOBALS['TL_LANG']['tl_content']['tm_use_fixed_task'],
	'exclude'                 => false,
	'inputType'               => 'checkbox',
	'eval'                    => array('submitOnChange'=>true)
);

class tl_content_tsk extends Backend
{
	public function getProjects()
	{
		$objProjects = $this->Database->prepare("SELECT id, name FROM tl_tm_projects")->execute();

		if ($objProjects->numRows < 1)
		{
			return array();
		}

		$return = array();

		while ($objProjects->next())
		{
			$return[$objProjects->id] = $objProjects->name;
		}

		return $return;
	}
	
	public function getTasks()
	{
		$objTasks = $this->Database->prepare("SELECT id, name FROM tl_tm_tasks")->execute();

		if ($objTasks->numRows < 1)
		{
			return array();
		}

		$return = array();

		while ($objTasks->next())
		{
			$return[$objTasks->id] = $objTasks->name;
		}

		return $return;
	}
}
?>